package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Holiday;
import com.xmy.cultivate.service.IHolidayService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 节假日 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-08-23
 */
@RestController
@RequestMapping("/api/holiday")
@Scope("prototype")
public class HolidayController {

    @Autowired
    IdWorker idWorker;

    @Autowired
    IHolidayService iHolidayService;

    @GetMapping()
    public Result findAll(@RequestParam Map<String,String> reMap,
                          @RequestParam(value = "type",required = false,defaultValue = "1") Integer type){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<Holiday> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<Holiday> holidayQueryWrapper = new QueryWrapper<>();
        String name = reMap.get("name");
        String yearPart = reMap.get("yearPart");
        holidayQueryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        holidayQueryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);
        holidayQueryWrapper.eq("type",1);
        iHolidayService.page(iPage,holidayQueryWrapper);
        return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id){
        try{
            Holiday holiday = iHolidayService.getById(id);
            return new Result(Code.OK,holiday,"");
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }


    @PostMapping()
    public Result save(@RequestBody Holiday holiday){
        holiday.setYearPart(holiday.getStartDate().getYear());
        if (holiday.getType() == null){
            holiday.setType(1);
        }
        boolean rs = iHolidayService.save(holiday);
        return new Result(rs?Code.OK:Code.ERROR,"","");
    }

    @PutMapping()
    public Result update(@RequestBody Holiday holiday){
        try{
            holiday.setYearPart(holiday.getStartDate().getYear());
            if (holiday.getType() == null){
                holiday.setType(1);
            }
            boolean rs = iHolidayService.updateById(holiday);
            return new Result(rs?Code.OK:Code.ERROR,"","");
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        try{
            boolean rs =  iHolidayService.removeById(id);
            return new Result(rs?Code.OK:Code.ERROR,"","");
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }
}
